#include "./parts.h"
#include "../device/gpio.h"

namespace driver {

using namespace device::gpio;

using PUMP_WATER = PB<10>;
using PUMP_STEAM = PB<11>;
using VALVE4 = PB<12>;
using VALVE3 = PB<13>;
using VALVE2 = PB<14>;
using VALVE1 = PB<15>;
using PUMP_OUTLET = PA<0>;

using PB8 = PB<8>;
using PB9 = PB<9>;
using PB1 = PB<1>;

Parts::Parts() {

    RCC->APB2ENR |=
        RCC_APB2ENR_IOPAEN +
        RCC_APB2ENR_IOPBEN;
    config_pp_out<
        PUMP_WATER,
        PUMP_STEAM,
        VALVE4,
        VALVE3,
        VALVE2,
        VALVE1,
        PUMP_OUTLET,
        PB8,
        PB9,
        PB1
    >();
}

void Parts::heat_close() {
    PB1::clr();
    PB9::clr();
    PB8::clr();
    m_steam_heat_flag = false;
    m_water_heat_flag = false;
}

void Parts::steam_heat_close() {
    PB8::clr();
    m_steam_heat_flag = false;
}

void Parts::steam_heat_open() {
    PB9::clr();
    PB8::set();
    m_water_heat_flag = false;
    m_steam_heat_flag = true;
}

void Parts::water_heat_close() {
    PB1::clr();
    m_water_heat_flag = false;
}

void Parts::water_heat_open() {
    PB9::set();
    PB1::set();
    m_steam_heat_flag = false;
    m_water_heat_flag = true;
}

void Parts::valve_open(uint8_t id) {
    switch (id) {
        case 1:
            VALVE1::set();
            m_valve1_flag = true;
        break;

        case 2:
            VALVE2::set();
            m_valve2_flag = true;
        break;

        case 3:
            VALVE3::set();
            m_valve3_flag = true;
        break;

        case 4: 
            VALVE4::set();
            m_valve4_flag = true;
        break;
    }
}

void Parts::valve_close(uint8_t id) {
    switch (id) {
        case 1:
            VALVE1::clr();
            m_valve1_flag = false;
        break;

        case 2:
            VALVE2::clr();
            m_valve2_flag = false;
        break;

        case 3: 
            VALVE3::clr();
            m_valve3_flag = false;
        break;

        case 4:
            VALVE4::clr();
            m_valve4_flag = false;
        break;
    }
}

void Parts::steam_pump_open() {
    PUMP_STEAM::set();
    m_steam_pump_flag = true;
}

void Parts::steam_pump_close() {
    PUMP_STEAM::clr();
    m_steam_pump_flag = false;
}

void Parts::water_pump_open() {
    PUMP_WATER::set();
    m_water_pump_flag = true;
}

void Parts::water_pump_close() {
    PUMP_WATER::clr();
    m_water_pump_flag = false;
}

void Parts::outlet_pump_open() {
    PUMP_OUTLET::set();
    m_outlet_pump_flag = true;
}

void Parts::outlet_pump_close() {
    PUMP_OUTLET::clr();
    m_outlet_pump_flag = false;
}

}
